package com.lineage.server.command.executor;

import com.lineage.server.datatables.EventSpawnTable;
import com.lineage.server.datatables.NpcSpawnTable;
import com.lineage.server.datatables.QuesttSpawnTable;
import com.lineage.server.datatables.SpawnTable;
import com.lineage.server.datatables.lock.SpawnBossReading;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Spawn;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1ToSpawn implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1ToSpawn.class);
  
  public static L1CommandExecutor getInstance() {
    return new L1ToSpawn();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      StringTokenizer st = new StringTokenizer(arg);
      int id = Integer.parseInt(st.nextToken());
      L1Spawn spawn = NpcSpawnTable.get().getTemplate(id);
      if (spawn == null)
        spawn = SpawnTable.get().getTemplate(id); 
      if (spawn == null)
        spawn = EventSpawnTable.get().getTemplate(id); 
      if (spawn == null)
        spawn = SpawnBossReading.get().getTemplate(id); 
      if (spawn == null)
        spawn = QuesttSpawnTable.get().getTemplate(id); 
      if (spawn != null) {
        L1Teleport.teleport(pc, spawn.getTmpLocX(), spawn.getTmpLocY(), spawn.getTmpMapid(), 5, false);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("移动至指定召唤编号: " + id));
      } else {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("没有这个编号的召唤点: " + id));
      } 
    } catch (Exception e) {
      _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName(), e);
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
    } 
  }
}
